const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
//获取应用实例
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'https://image.xingqubaike.com:8443/minicard/ucenter/personcenter/background/scene/1.png',
      'https://image.xingqubaike.com:8443/minicard/ucenter/personcenter/background/scene/2.png',
      'https://image.xingqubaike.com:8443/minicard/ucenter/personcenter/background/scene/3.png'
    ],
    categorySelected: {categoryid:"", categoryname: "热门"},// 选中的类目标签
    list: [],// 专辑数据
    categoryPopupShow: false,// 控制类目标签是否展示
    categoryList: [],// 类目标签
    // 自定义自己喜欢的颜色
    colorArr: ["#fd5660", "#fea350", "#fbe136", "#7ec58b", "#ed8442", "#fccc69", "#bbd341", "#1da88b", "#ffc607", "#42ef4a", "#42edc3", "#6aade2", "#26a70c", "#08bdd7", "#0272ea", "#067faa", "#069bd3", "#0d5094", "#3f8ad6", "#3252f8"],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var categoryid = options.categoryid;
    var categoryname = options.categoryname;
    var categorySelected = {};
    categorySelected.categoryid = categoryid;
    categorySelected.categoryname = categoryname;
    _this.setData({
      categorySelected: categorySelected
    })
    // 获取专辑数据
    _this.getWeeklyInfo();

    // 获取类目
    _this.levelOneCategory();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取专辑
   */
  getWeeklyInfo: function () {
    var _this = this;
    var data = {};
    data.pageNum = 1;
    data.pageSize = 200;
    var url = api.Home_SearchWeeklyInfoUrl;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        url = api.Home_SearchWeeklyInfoApiUrl;
        _this.getWeeklyInfoQuery(url, data);
      })
    } else {
      _this.getWeeklyInfoQuery(url, data);
    }
  },

  /**
   * 获取专辑：请求
   */
  getWeeklyInfoQuery: function (url, data) {
    var _this = this;
    util.post(url, data).then(function (res) {
      if (res.code == 200) {
        var list = res.result.list;
        _this.setData({
          list: list
        })
      }
    })
  },

  /**
   * 专辑类目选择
   */
  categoryBind: function () {
    let that = this;
    var categoryPopupShow = that.data.categoryPopupShow;
    if (categoryPopupShow) {
      that.setData({
        categoryPopupShow: false
      })
    } else {
      that.setData({
        categoryPopupShow: true
      })
    }
  },

  /**
   * 搜索你感兴趣的专辑
   */
  navigateToSearchAlbum: function () {
    wx.navigateTo({
      url: '../searchAlbum/searchAlbum',
    })
  },
  
  /**
   * 获取类目导航信息
   */
  levelOneCategory: function () {
    var _this = this;
    util.post(api.Home_LevelOneCategoryUrl).then(function (res) {
      if (res.code == 200) {
        var categoryList = res.result.list;
        var bb = {};
        bb.id = "";
        bb.parentid = "0";
        bb.catalogname = "宝贝专辑";
        bb.uncheakedimage = "/minicard/home/weekly/album/album_baobeizuanji.png";
        categoryList.push(bb);
        var xcx = {};
        xcx.id = "";
        xcx.parentid = "0";
        xcx.catalogname = "小程序专辑";
        xcx.uncheakedimage = "/minicard/home/weekly/album/album_xiaochengxuzuanji.png";
        categoryList.push(xcx);
        var cb = {};
        cb.id = "";
        cb.parentid = "0";
        cb.catalogname = "橙贝商城";
        cb.uncheakedimage = "/minicard/home/weekly/album/album_chengbeishangcheng.png";
        categoryList.push(cb);
        _this.setData({
          categoryList: categoryList
        })
      }
    })
  },

  /*
   * 专辑类目选中
   */
  categorySelected: function(e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    var catalogid = e.currentTarget.dataset.catalogid;
    var catalogname = e.currentTarget.dataset.catalogname;
    var categorySelected = {};
    categorySelected.catalogid = catalogid;
    categorySelected.catalogname = catalogname;
    _this.setData({
      categorySelected: categorySelected
    })
    // 关闭弹窗
    _this.categoryBind();
    if ((catalogid != null && catalogid.length > 0) || (catalogname != null && catalogname != "橙贝商城")){
      _this.navigateToInterestAlbum();
    }
  },

  /**
   * 查看更多，跳转到兴趣专辑页面
   */
  navigateToInterestAlbum: function () {
    var _this = this;
    var categorySelected = _this.data.categorySelected;
    var pcatalogid = categorySelected.catalogid;
    var pcatalogname = categorySelected.catalogname;
    if (pcatalogid == null || pcatalogid.length <= 0) {
      pcatalogid = "";
    }
    if (pcatalogname == null || pcatalogname.length <= 0) {
      pcatalogname = "";
    }
    wx.navigateTo({
      url: '../interestAlbum/interestAlbum?pcatalogid=' + pcatalogid + '&pcatalogname=' + pcatalogname,
    })
  },

  /**
   * 返回上一页
   */
  navigateBack: function(){
    wx.navigateBack({
      delta: 1
    })
  }
})